unit uWork_With_INDEXES;
interface

uses
    TEClientLib_TLB, Classes;

	type 
		IndexStruc = record
			Name,
			ShortName:WideString;
			CurrentValue:Double;
		end;
	
	TINDEXES = class(TObject)
        private
            OId:Integer;
            SlotTableReplied:Boolean;
            SlotTableOpenedF:Boolean;
            SlotTable:TSlotTable;
            CurrentValue:IndexStruc;
            fOnUpdated:TNotifyEvent;
            fMainError:Boolean;

			procedure SlotTableAddRow(ASender: TObject; OpenID, RowID: Integer; Fields: OleVariant);
			procedure SlotTableUpdateRow(ASender: TObject; OpenID, RowID: Integer;Fields: OleVariant);
            procedure SlotTableReplEnd(ASender: TObject; OpenID: Integer);
            procedure SlotTableOpened(ASender: TObject; OpenID: Integer);
        public
            constructor Create;
            destructor Free;

			procedure OpenTable(sltId:Word; ShortName:WideString);

            property Value:Double read CurrentValue.CurrentValue;
            property Name:WideString read CurrentValue.Name;
			property ShortName:WideString read CurrentValue.ShortName;
            property Replied:Boolean read SlotTableReplied;
            property OnUpdated:TNotifyEvent read fOnUpdated write fOnUpdated;
            property MainError:Boolean read fMainError write fMainError; 

    end;

implementation


procedure TINDEXES.SlotTableOpened(ASender: TObject; OpenID: Integer);
begin
    SlotTableOpenedF:=True;
    OId:=OpenId;    
end;

procedure TINDEXES.SlotTableReplEnd(ASender: TObject; OpenID: Integer);
begin
    SlotTableReplied:=True;
end;

procedure TINDEXES.SlotTableAddRow(ASender: TObject; OpenID,RowID: Integer; Fields: OleVariant);
begin
	if (Fields[2] = CurrentValue.ShortName) then
	begin
		CurrentValue.Name := Fields[1];
		CurrentValue.ShortName := Fields[2];
		CurrentValue.CurrentValue := Fields[3];
        if Assigned(fOnUpdated) then fOnUpdated(Self);        
	end;
end;

procedure TINDEXES.SlotTableUpdateRow(ASender: TObject; OpenID, RowID: Integer;Fields: OleVariant);
begin
	if (Fields[2] = CurrentValue.ShortName) then
	begin
		CurrentValue.Name := Fields[1];
		CurrentValue.ShortName := Fields[2];
		CurrentValue.CurrentValue := Fields[3];
        if Assigned(fOnUpdated) then fOnUpdated(Self);
	end;
end;

constructor TINDEXES.Create;
begin
	inherited;
	MainError:=False;

    SlotTableReplied:=False;
    SlotTableOpenedF:=False;
    SlotTable:=TSlotTable.Create(nil);
    SlotTable.OnAddRow:=SlotTableAddRow;
	SlotTable.OnUpdateRow:=SlotTableUpdateRow;
    SlotTable.OnReplEnd:=SlotTableReplEnd;
    SlotTable.OnOpened:=SlotTableOpened;    
end;

destructor TINDEXES.Free;
begin
    if not(MainError) then
        SlotTable.Close(OId);
	SlotTable.Free;
end;

procedure TINDEXES.OpenTable(sltId:Word; ShortName:WideString);
var
	FNames:WideString;
begin
	CurrentValue.ShortName:=ShortName;
	
	FNames:='ID,Name,ShortName,CurrentValue';

    SlotTable.FieldNames := FNames;
    SlotTable.Open(sltId, 'INDEXES', '');
end;

end.
